const scpClient = require('scp2');
const ora = require('ora');
const chalk = require('chalk');
const Client = require('ssh2').Client;
const baseDir = 'nbcockpit';
const spinner = ora(chalk.green('正在发布到测试服务器...'));

function deployFile() {
    const service = {
        host: '192.168.18.14', // 服务器IP
        port: '22', // 服务器端口
        username: 'qianduan', // 服务器ssh登录用户名
        password: 'qianduan', // 服务器ssh登录密码
        path: '/home/kong/www/' + baseDir // 服务器web目录
    };
    const conn = new Client();

    conn.on('ready', () => {
        // 删除上个版本的文件
        conn.exec('rm -rf /home/kong/www/' + baseDir + '/*', (err, stream) => {
            if (err) throw err;
            stream.on('close', (code, signal) => {
                spinner.start();
                scpClient.scp('./' + baseDir + '/', service, err => {
                    spinner.stop();
                    if (!err) {
                        console.log(chalk.green('测试服务器部署完毕。'));
                    } else {
                        console.log('err', err);
                    }
                });
                conn.end();
            }).on('data', (data) => {
                console.log('STDOUT: ' + data);
            }).stderr.on('data', (data) => {
                console.log('STDERR: ' + data);
            });
        });
    }).connect(service);
}
deployFile();
